package org.sgx.gwteditors.yui.simple;

import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.EditorValueChangeEvent;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.impl1.basetypes.InRangeDouble;
import org.sgx.yuigwt.yui.YUICallback;
import org.sgx.yuigwt.yui.YuiContext;
import org.sgx.yuigwt.yui.YuiLoader;
import org.sgx.yuigwt.yui.event.EventCallback;
import org.sgx.yuigwt.yui.widget.slider.Slider;
import org.sgx.yuigwt.yui.widget.slider.SliderConfig;
import org.sgx.yuigwt.yui.widget.slider.SliderEvent;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class InRangeDoubleYuiSliderEditor extends Composite 
implements YUICallback, Editor<InRangeDouble> {
	
	static String[] yuiModules = {"slider"};  

	private static InRangeDoubleYuiSliderEditorUiBinder uiBinder = GWT
			.create(InRangeDoubleYuiSliderEditorUiBinder.class);

	interface InRangeDoubleYuiSliderEditorUiBinder extends UiBinder<Widget, InRangeDoubleYuiSliderEditor> {
	}
	
	@UiField Element el;

	private Slider slider;

	private InRangeDouble model = null;
	

	public InRangeDoubleYuiSliderEditor() {
		initWidget(uiBinder.createAndBindUi(this));
		YuiLoader.load(yuiModules, this);
	}

	@Override
	public void ready(YuiContext Y) {
//		System.out.println("RRRRR");
		this.slider = Y.newSlider(SliderConfig.create().srcNode(el)); 
		this.slider.on(Slider.EVENT_SLIDEEND, new EventCallback<SliderEvent>() {
			@Override
			public void call(SliderEvent e) {
				EditorValueChangeEvent<InRangeDouble> ev_ = new EditorValueChangeEvent<InRangeDouble>(InRangeDoubleYuiSliderEditor.this, flush(), null);		
				EditorEventManager.<InRangeDouble>getInstance().fireValueChangeEvent(InRangeDoubleYuiSliderEditor.this, ev_);
			}
		});
		this.slider.render(); 
		if(model!=null)
			slider.setValue(getSliderValue(model.getValue()));
	}
	
	@Override
	public Widget getWidget() {
		return this; 
	}
	
	@Override
	public InRangeDouble flush() {
		double sv = slider.getDouble("value"); 
		double real = getRealValue(sv); 
		model.setValue(real); 
		return model; 
	}
	@Override
	public void load(InRangeDouble model) {
		this.model=model;
		if(slider!=null)
			slider.setValue(getSliderValue(model.getValue()));//(", val)setRealValue(model.getValue()); 
	}
	
	

	@Override
	public void addValueChangeListener(ValueChangeListener<InRangeDouble> l) {
		EditorEventManager.<InRangeDouble>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<InRangeDouble> l) {
		EditorEventManager.<InRangeDouble>getInstance().removeValueChangeListener(this, l);
	}
	private double getRealValue(double value) {
		double rv = (value/100.0)*(model.getMax()-model.getMin())+model.getMin();
		return rv; 
	}
	private int getSliderValue(double value) {
		double rv = (value/100.0)*(model.getMax()-model.getMin())-model.getMin(); 
		return (int)rv; 
	}
	
	@Override
	public List<EditorError<InRangeDouble>> getErrors() {
		// TODO Auto-generated method stub
		return null;
	}

	

	@Override
	public boolean isSimple() {
		return true;
	}

	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[]{EditorFramework1.TYPE_INRANGEDOUBLE}; 
	}

}
